<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit">
<title>浏览器-会员分析</title>

<script type="text/javascript" src="../resources/js/contants.js"></script>
<link href="../resources/css/bootstrap.min.css" rel="stylesheet">
<link href="../resources/css/font-awesome.min.css" rel="stylesheet">
<link href="../resources/css/style.min.css" rel="stylesheet">
<link href="../resources/css/dateRange.css" rel="stylesheet">
<link href="../resources/css/bootstrap-select.min.css" rel="stylesheet">
<link rel="stylesheet" href="../resources/css/layer.css" id="layui_layer_skinlayercss" style="">
<link rel="stylesheet" href="../resources/css/layer.ext.css" id="layui_layer_skinlayercss" style="">
<link rel="stylesheet" href="../resources/css/style.css" id="layui_layer_skinlayercss" style="">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-info">公用过滤条件</span>
                    </div>
                    <div class="ibox-content">
                        <span class="font-size-h1">操作系统名称:
                            <select id="browser_select">
                            </select>
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-success pull-right">昨日</span>
                        <h5>新增会员</h5>
                    </div>
                    <div class="ibox-content">
                        <h1 class="no-margins"><span id="browser_new_members">-</span></h1>
                        <div class="stat-percent font-bold fa-lg">
                          <span id="browser_new_member_rate" class="fa"></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-success pull-right">昨日</span>
                        <h5>活跃会员</h5>
                    </div>
                    <div class="ibox-content">
                        <h1 class="no-margins"><span id="browser_active_members">-</span></h1>
                        <div class="stat-percent font-bold">
                          <span id="browser_active_member_rate" class="fa"></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <span class="label label-success pull-right">昨日</span>
                        <h5>总会员</h5>
                    </div>
                    <div class="ibox-content">
                        <h1 class="no-margins"><span id="browser_total_members">-</span></h1>
                        <div class="stat-percent font-bold text-navy">
                          <span id="browser_total_member_rate" class="fa"></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div id="demo_title" class="col-sm-12 no-margins">
              <div class="row no-paddings no-margins no-border">
                <div class="col-sm-12 no-margins" style="padding:0px; border:0px;">
                  <div class="ibox no-margins">
                    <div class="ibox-content">
                        <span class="no-margins">选择时间：</span>
                        <button id="time_show_bt" type="button" class="btn btn-default no-margins no-borders no-paddings" style="padding-bottom: 0px">
                            <span id="date1">默认</span>
                            <span class="caret"></span>
                        </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div id="demo" class="col-sm-12"></div>
            <div id="demo1" class="col-sm-12"></div>
            <div id="demo2" class="col-sm-12"></div>
            <div id="demo3" class="col-sm-12"></div>
        </div>
    </div>

    <script type="text/javascript" src="../resources/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="../resources/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../resources/js/dateRange.js"></script>
    <script type="text/javascript" src="../resources/js/bootstrap-select.min.js"></script>
    <!-- highcharts js -->
    <script type="text/javascript" src="../resources/js/highcharts.js"></script>
    <script type="text/javascript" src="../resources/js/exporting.js"></script>
    <script type="text/javascript" src="../resources/js/json2.js"></script>
    <script type="text/javascript" src="../resources/js/common.js"></script>

    <script type="text/javascript">
        Highcharts.setOptions({
            tooltip: {
                pointFormat: '<span stype="color:{series.color}">{series.name}</span>:<b>{point.y:,0.0001f}</b><br/>',
            },
        });
        var startDate = getStartDate();
        var endDate = getEndDate();

        $("#browser_select").change(function(){
            var name = $(this).val();
            browserChangeEvent(name);
        });

        function browserChangeEvent(browserName) {
            var sd = date2YmdStr(getYesterDay());
            var ed = sd;
            loadHeadData(sd, ed, browserName);
            loadContent(getStartDate(), getEndDate(), browserName);
        };

        function showSpeedRate(showId, value) {
            if (value) {
                if (value > 0) {
                    $(showId).html(value.toPercent() + "%");
                    $(showId).addClass("text-danger");
                    $(showId).addClass("fa-level-up");
                } else {
                    value = value * -1;
                    $(showId).html(value.toPercent() + "%");
                    $(showId).addClass("text-success")
                    $(showId).addClass("fa-level-down");
                }
            } else {
                $(showId).html("-");
                $(showId).addClass("text-info");
            }
        }

        function loadHeadData(sd, ed, browser) {
            var url = COMMON_PREFIX_URL + "/stats/summary/browser?metric=total_members,active_members,new_members&platform=website";
            url += "&start_date=" + sd + "&end_date=" + ed + "&browser=" + browser;
            $.get(url, {}, function(data){
                if (data.code == '200' && data.data.length > 0) {
                    var membersData = data.data[0];
                    var nu = membersData["$new_members"];
                    if (typeof(nu) != "undefined") {
                        $("#browser_new_members").html(nu.formatThousands());
                    }
                    var au = membersData["$active_members"];
                    if (typeof(au) != "undefined") {
                        $("#browser_active_members").html(au.formatThousands());
                    }
                    var tu = membersData["$total_members"];
                    if (typeof(tu) != "undefined") {
                        $("#browser_total_members").html(tu.formatThousands());
                    }
                } else {
                    $("#browser_new_members").html("-");
                    $("#browser_active_members").html("-");
                    $("#browser_total_members").html("-");
                }
            });
        };

        function loadContent(sd, ed, browser) {
            var url = COMMON_PREFIX_URL + "/stats/groupBy/browser?metric=new_members,active_members,total_members&platform=website";
            var gkey = "browser_name";
            url += "&start_date=" + sd + "&end_date=" + ed;
            if (browser == 'all') {
                url += "&group_by=day,browser";
            } else {
                url += "&group_by=day,browser_version&browser=" + browser;
                gkey = "browser_version";
            }

            $.get(url, {}, function(data) {
                cleanContent("demo");
                cleanContent("demo1");
                cleanContent("demo2");
                cleanContent("demo3");

                if (data.code == '200') {
                    if (data.data.length == 0) {
                       showNoConnent("demo");
                       return;
                    }

                    var chart = new Highcharts.Chart({
                        chart : {
                            renderTo : 'demo', // 在那个标签上显示，指定id即可
                            type : "column" // 类型
                        },
                        xAxis : {
                            title : {
                                text : "日期"
                            }
                        },
                        title: {
                            text: "活跃活跃、新会员、总会员比较图"
                        },
                        yAxis : [{
                            min: 0,
                            title : {
                                text : "用户数量"
                            }
                        }],
                        series : []
                    });
                    var chart1 = new Highcharts.Chart({
                        chart: {
                            renderTo: 'demo1',
                            type: 'area'
                        },
                        title: {
                            text: '新会员组成'
                        },
                        xAxis: {
                            tickmarkPlacement: 'on',
                            title: {
                                enabled: false
                            }
                        },
                        yAxis: {
                            title: {
                                text: '百分比'
                            }
                        },
                        tooltip: {
                            pointFormat: '<span stype="color:{series.color}">{series.name}</span>:<b>{point.percentage:0.1f}%</b> ({point.y:,.0f})<br/>',
                            shared: true
                        },
                    });
                    var chart2 = new Highcharts.Chart({
                        chart: {
                            renderTo: 'demo2',
                            type: 'area'
                        },
                        title: {
                            text: '活跃会员组成'
                        },
                        xAxis: {
                            tickmarkPlacement: 'on',
                            title: {
                                enabled: false
                            }
                        },
                        yAxis: {
                            title: {
                                text: '百分比'
                            }
                        },
                        tooltip: {
                            pointFormat: '<span stype="color:{series.color}">{series.name}</span>:<b>{point.percentage:0.1f}%</b> ({point.y:,.0f})<br/>',
                            shared: true
                        },
                    });
                    var chart3 = new Highcharts.Chart({
                        chart: {
                            renderTo: 'demo3',
                            type: 'area'
                        },
                        title: {
                            text: '总会员组成'
                        },
                        xAxis: {
                            tickmarkPlacement: 'on',
                            title: {
                                enabled: false
                            }
                        },
                        yAxis: {
                            title: {
                                text: '百分比'
                            }
                        },
                        tooltip: {
                            pointFormat: '<span stype="color:{series.color}">{series.name}</span>:<b>{point.percentage:0.1f}%</b> ({point.y:,.0f})<br/>',
                            shared: true
                        },
                    });

                    var categories = [];
                    var keys = [];
                    var series = {};

                    $.each(data.data, function(i, item) {
                        var day = item["day"];
                        var browser = item[gkey];
                        var am = item["$active_members"];
                        var nm = item["$new_members"];
                        var tm = item["$total_members"];

                        series["am"] = series["am"] ? series["am"] : {};
                        series["nm"] = series["nm"] ? series["nm"] : {};
                        series["tm"] = series["tm"] ? series["tm"] : {};

                        series["am"][browser] = series["am"][browser] ? series["am"][browser] : {name:browser, data_ext:{}, data:[], stack: 'active_members'};
                        series["nm"][browser] = series["nm"][browser] ? series["nm"][browser] : {name:browser, data_ext:{}, data:[], stack: 'new_members'};
                        series["tm"][browser] = series["tm"][browser] ? series["tm"][browser] : {name:browser, data_ext:{}, data:[], stack: 'total_members'};

                        series["am"][browser]["data_ext"][day] = am;
                        series["nm"][browser]["data_ext"][day] = nm;
                        series["tm"][browser]["data_ext"][day] = tm;

                        keys.push(browser);
                        categories.push(day);
                    });
                    categories = uniqueArray(categories);
                    keys = uniqueArray(keys);

                    var nms = {
                        name: "新会员",
                        data: []
                    };
                    var ams = {
                        name: "活跃会员",
                        data: []
                    };
                    var tms = {
                        name: "总会员",
                        data: []
                    };
                    $(categories).each(function(i, v) {
                        nms["data"].push(0);
                        ams["data"].push(0);
                        tms["data"].push(0);
                    });
                    $(keys).each(function(i, v){
                    	$(categories).each(function(ii, vv) {
                    		series["am"][v]["data"].push(0);
                    		series["nm"][v]["data"].push(0);
                    		series["tm"][v]["data"].push(0);
                        });
                    	for (var item in series["nm"][v]["data_ext"]) {
                    		var index = indexOfValue(categories, item);
                    		var obj = series["nm"][v]["data_ext"];
                    		var num = 0;

                    		if (index > -1) {
                    			num = obj[item];
                    			nms["data"][index] += num;
                    			series["nm"][v]["data"][index] = num;
                    		}
                    	}
                    	for (var item in series["am"][v]["data_ext"]) {
                    		var index = indexOfValue(categories, item);
                    		var obj = series["am"][v]["data_ext"];
                    		var num = 0;

                    		if (index > -1) {
                    			num = obj[item];
                    			ams["data"][index] += num;
                    			series["am"][v]["data"][index] = num;
                    		}
                    	}
                    	for (var item in series["tm"][v]["data_ext"]) {
                    		var index = indexOfValue(categories, item);
                    		var obj = series["tm"][v]["data_ext"];
                    		var num = 0;

                    		if (index > -1) {
                    			num = obj[item];
                    			tms["data"][index] += num;
                    			series["tm"][v]["data"][index] = num;
                    		}
                    	}

                    	chart1.addSeries(series["nm"][v]);
                        chart2.addSeries(series["am"][v]);
                        chart3.addSeries(series["tm"][v]);
                    });
                    chart.addSeries(nms);
                    chart.addSeries(ams);
                    chart.addSeries(tms);
                    chart.xAxis[0].setCategories(categories);
                    chart1.xAxis[0].setCategories(categories);
                    chart2.xAxis[0].setCategories(categories);
                    chart3.xAxis[0].setCategories(categories);
                    return true;
                } else {
                    showNoConnent("demo", "获取数据失败，失败code为:" + data.code + ", 失败信息描述为:" + data.msg);
                }
                return false;
            }, "json");
        }

        $(document).ready(function(){
            // 加载浏览器基本信息
            var url = COMMON_PREFIX_URL + "/stats/getDimensions?dimensions=browser_name";
            $.get(url, {}, function(data){
                if (data.code == '200' && data.data != null && data.data["browser_name"].length > 0) {
                    var str = '<option value="all">all</option>';
                    $.each(data.data["browser_name"], function(i, item) {
                        if (item != 'all') {
                            str += '<option value="' + item["browser_name"] + '">' + item["browser_name"] + '</option>'
                        }
                    });
                    $("#browser_select").html(str);
                    $("#browser_select").addClass("selectpicker");
                    $('.selectpicker').selectpicker();
                } else {
                }
            });

            var sd = date2YmdStr(getYesterDay());
            var ed = sd;
            loadHeadData(sd, ed, "all");
            loadContent(startDate, endDate, "all");

            // 显示时间控件
            new AE_Date('date1','time_show_bt',function(startDate, endDate){
                loadContent(startDate, endDate, $("#browser_select").val());
            }, startDate, endDate);
        });
        
    </script>
</body>
</html>